package com.pansoft.openplanet.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pansoft.openplanet.R;
import com.utilcode.util.SizeUtils;


public class TCLoadingDataView {

    /*
     * 定义loadingDialog
     */
    private static Dialog loadingDialog;

    /*

    public static String stringShow;

    /*
     * 重载方法
     */
    public static void show(Context context) {
        if (context == null) {
            return;
        }
        if (!((Activity) context).isFinishing()){
            show(context, R.string.common_text_loading);

        }
    }

    public static void show(Context context, int msg) {
        String message = context.getResources().getString(msg);
        show(context, message);
    }

    /*
     * 显示dialog
     */
    public static void show(Context context, String msg) {
        if (context == null) {
            return;
        }
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        if (loadingDialog == null || !loadingDialog.isShowing()) {

            // 用来载入LinearLayout
            LayoutInflater layoutInflater = LayoutInflater.from(context);

            // 创建视图
            View v = layoutInflater.inflate(R.layout.talk_chain_view_loading, null);

            // 加载布局
            LinearLayout layout = v.findViewById(R.id.dialog_view_ios);

            // 加载imageView

            ImageView imageView = v.findViewById(R.id.loading_img_ios);
            //ImageView imageViewa = v.findViewById(R.id.loading_img_iosa);

            // 设置textVeiw
            TextView textView = v.findViewById(R.id.loading_textView_ios);
            if (msg.equals("")) {
                textView.setText(msg);

            } else {
                //textView.setText(msg+"..." );
                textView.setText(msg);

            }

            Resources res = context.getResources();
            float fontsize = res.getDimension(R.dimen.activity_login_button_fontsize);
//            textView.setTextSize(16);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

            //获取Imageview设置图片的宽度和高度
            // Bitmap bitmap = ((BitmapDrawable) imageViewa.getBackground()).getBitmap();


            AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();

            animationDrawable.start();
            // 加载动画
            //Animation loadingAnimation = AnimationUtils.loadAnimation(appContext,
            //R.anim.loading_animation);

            // 使用ImageView显示动画
            //imageView.startAnimation(loadingAnimation);
            //imageViewa.startAnimation(loadingAnimation);
            loadingDialog = new Dialog(context, R.style.loadingData_dialog);
            loadingDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            // 不可以用“返回键”取消
            // loadingDialog.setCancelable(false);
            //loadingDialog.setCancelable(true);
            loadingDialog.setContentView(layout);

            //设置dialog的大小
            Window dialogWindow = loadingDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = SizeUtils.dp2px(60) + textView.getLineHeight() + 240;
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            textView.measure(w, h);
//            lp.height = SizeUtils.dp2px(60) + textView.getMeasuredHeight() + 75;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

            dialogWindow.setAttributes(lp);

		/*loadingDialog.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);*/
            if (!((Activity) context).isFinishing())
                loadingDialog.show();
        }
    }

    /*
     * 取消dialog
     */

    public static void dismiss() {
//        if (loadingDialog != null){
//            loadingDialog.dismiss();
//            loadingDialog=null;
//        }
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                Context context = ((ContextWrapper) loadingDialog.getContext()).getBaseContext();
                if (context instanceof Activity) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        if (!((Activity) context).isDestroyed())
                            loadingDialog.dismiss();
                    }
                } else {
                    loadingDialog.dismiss();
                }
            }
            loadingDialog = null;
        }
    }

    /*
     * 取消dialog
     */

    public static Dialog getDialog() {
        return loadingDialog;
    }


}